#import "ALSectionColumnEntry.h"
#import "NSObject+BlockObservation.h"
#import "ALVersionView.h"
#import "ALEventView.h"


#pragma mark Constants


#pragma mark -
#pragma mark Class Globals

static NSColor *ALSectionBottomBorderNormal;
static NSColor *ALSectionBottomBordersection;
static NSColor *ALSectionBottomBorderSelected;


#pragma mark -
@interface ALSectionColumnEntry (ALsectionColumnEntryPrivateMethods)
-(ALVersionView *)sectionView_;
@end


#pragma mark -
@implementation ALSectionColumnEntry

+(void)initialize {
	ALSectionBottomBorderNormal = [NSColor colorWithCalibratedWhite:0.9 alpha:1.000];
	ALSectionBottomBordersection = [NSColor colorWithCalibratedWhite:0.696 alpha:1.000];
	ALSectionBottomBorderSelected = [NSColor colorWithCalibratedRed:0.473 green:0.526 blue:0.606 alpha:1.000];
}


#pragma mark -
#pragma mark Notifications

// Event views draw only their bottom border.  This method iterates through the section's child event entries and assigns the appropriate color of each event's border (and the section's border) based on adjacent events and sections and their selection state
-(void)updateChildEntryColorAssignments:(NSNotification *)note {
	for (NSUInteger entryIndex = 0; entryIndex < [[self childEntries] count]; entryIndex++) {
		NSColor *bottomBorderColor = nil;
		SMRColumnEntry *topEntry = [[self childEntries] objectAtIndex:entryIndex];
		SMRColumnEntry *bottomEntry = nil;
		if (topEntry != [[self childEntries] lastObject]) 
			bottomEntry = [[self childEntries] objectAtIndex:entryIndex + 1];
		if ([topEntry selected] || [bottomEntry selected])
			bottomBorderColor = ALSectionBottomBorderSelected;
		else if (!bottomEntry)
			bottomBorderColor = ALSectionBottomBordersection;
		else 
			bottomBorderColor = ALSectionBottomBorderNormal;
		
		[(ALEventView *)[topEntry view] setBottomBorderColor:bottomBorderColor];
	}
	NSColor *sectionBorderColor = ALSectionBottomBordersection;
	if ([[self childEntries] count] != 0 && [[[self childEntries] objectAtIndex:0] selected])
		sectionBorderColor = ALSectionBottomBorderSelected;
	[[self sectionView_] setBottomBorderColor:sectionBorderColor];
}


#pragma mark -
#pragma mark Column entry overrides

-(void)columnEntryWasAddedToColumnView {
	[super columnEntryWasAddedToColumnView];
	[self setAutomaticallyRearrangesObjects:YES];
	[[self sectionView_] setHeaderHeight:[self topPadding]];
	[[self sectionView_] setBottomBorderColor:ALSectionBottomBordersection];
	NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
	[noteCenter addObserver:self selector:@selector(updateChildEntryColorAssignments:) name:SMRColumnViewChildEntriesDidChangeNotification object:[self columnView]];
	[noteCenter addObserver:self selector:@selector(updateChildEntryColorAssignments:) name:SMRColumnViewSelectionDidChangeNotification object:[self columnView]];
}


#pragma mark -
#pragma mark Private

-(ALVersionView *)sectionView_ {
	return (ALVersionView *)[self view];
}

@end
